load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_license//rules:license.bzl", "license")

package(
    default_applicable_licenses = [
        ":license.APACHE",
        ":license.BSD",
        ":license.MINPACK",  # Only used by unsupported/** not by Eigen/**.
        ":license.MPL2",
    ],
)

exports_files(glob(["COPYING.*"]))

# Note: Eigen is primarily an MPL2 library with small bits of code under other
# licenses. We've taken care to not glob any LGPL code and set EIGEN_MPL2_ONLY
# to fail at compile-time in case any leaks in, so these four licenses are the
# relevant files for the code in our cc_library.

license(
    name = "license.APACHE",
    license_kinds = ["@rules_license//licenses/spdx:Apache-2.0"],
    license_text = "COPYING.APACHE",
)

license(
    name = "license.BSD",
    license_kinds = ["@rules_license//licenses/spdx:BSD-3-Clause"],
    license_text = "COPYING.BSD",
)

license(
    name = "license.MINPACK",
    license_kinds = ["@rules_license//licenses/spdx:BSD-3-Clause-Attribution"],
    license_text = "COPYING.MINPACK",
)

license(
    name = "license.MPL2",
    license_kinds = ["@rules_license//licenses/spdx:MPL-2.0"],
    license_text = "COPYING.MPL2",
)

EIGEN_FILES = [
    "Eigen/**",
    "unsupported/Eigen/**",
]

# LGPL files
EIGEN_RESTRICTED_FILES = [
    "unsupported/Eigen/src/IterativeSolvers/IterationController.h",
    "unsupported/Eigen/src/IterativeSolvers/ConstrainedConjGrad.h",
]

EIGEN_MPL2_HEADER_FILES = glob(
    EIGEN_FILES,
    exclude = EIGEN_RESTRICTED_FILES,
)

cc_library(
    name = "eigen",
    hdrs = EIGEN_MPL2_HEADER_FILES,
    defines = ["EIGEN_MPL2_ONLY"],
    includes = ["."],
    visibility = ["//visibility:public"],
)
